package com.trkj.controller;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.trkj.pojo.Emp;
import com.trkj.service.EmailService;
import com.trkj.vo.BirthdetailVo;
import com.trkj.vo.BirthempVo;
import com.trkj.vo.EmpUseByemailVo;
import com.trkj.vo.Information;
import com.trkj.vo.MakedEmailVo;
import com.trkj.vo.NewcopyemailVo;
import com.trkj.vo.NewemailVo;
import com.trkj.vo.PropertyVo;
import com.trkj.vo.ReserveVo;

/**
 * 
 * @author 23153黎凯
 *
 */
@Controller
@Transactional
@SessionAttributes({"emp", "funcIds"})
public class EmailController {
	@Autowired
	EmailService emailService;
	
	
	/**
	 * 视图层保存了emp信息、时间段信息，用于邮箱主显示页面
	 * @param model
	 * @return
	 * @throws ParseException 
	 */
	@RequestMapping("/to_emaill")
	public String to_emaill(Model model) throws ParseException {
		EmpUseByemailVo empUseByemailVo=new EmpUseByemailVo();
		Emp emp = (Emp) model.asMap().get("emp");
	    Integer empid =emp.getEmpId();
		empUseByemailVo=emailService.selectempinfoEmail(empid);
		model.addAttribute("empUseByemailVo", empUseByemailVo);
		return "to_emaill";
	}
	
	/**
	 * 跟新用户邮箱信息
	 * @param model
	 * @param email
	 * @return
	 */
	@PostMapping("/modifyemail")
    @ResponseBody
    public Information editEmpPassword(Model model,String email){
        try {
            Emp emp = (Emp) model.asMap().get("emp");
            Integer empid = emp.getEmpId();
            boolean b = emailService.modifyemail(empid, email); 
            if(b){
                return new Information();
            }else{
                return Information.getInstance(false, "更新失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Information.getInstance(false, e.getMessage());
        }
    }
	
	@RequestMapping("/to_makedemailjsp")
	public String to_makedemailjsp() {
		return "to_makedemailjsp";
	}
	
	/**
	 * 已发送查询
	 * @param model
	 * @return
	 */
	@RequestMapping("/to_makedemailt")
	@ResponseBody
	public Map<String, Object> to_makedemail(Model model) {
		Emp emp = (Emp) model.asMap().get("emp");
        Integer empid = emp.getEmpId();
        List<MakedEmailVo> makedEmailVos=emailService.querymakedemailb(empid);
		Map<String, Object> map = new HashMap<>();
		map.put("total", makedEmailVos.size());
        map.put("rows", makedEmailVos);
		return map;
	}
	
	
	@RequestMapping("/to_deletedemailjsp")
	public String to_deletedemailjsp() {
		return "to_deletedemailjsp";
	}
	/**
	 * 垃圾箱查询
	 * @param model
	 * @return
	 */
	@RequestMapping("/to_deletedemail")
	@ResponseBody
	public Map<String, Object> to_deletedemail(Model model) {
		Emp emp = (Emp) model.asMap().get("emp");
        Integer empid = emp.getEmpId();
        List<MakedEmailVo> deletedEmailVos=emailService.querydeletedemail(empid);
		Map<String, Object> map = new HashMap<>();
		map.put("total", deletedEmailVos.size());
        map.put("rows", deletedEmailVos);
		return map;
	}
	
	
	@RequestMapping("/to_savedemailjsp")
	public String to_savedemailjsp() {
		return "to_savedemailjsp";
	}
	/**
	 * 暂存箱查询
	 * @param model
	 * @return
	 */
	@RequestMapping("/to_savedemail")
	@ResponseBody
	public Map<String, Object> to_savedemail(Model model) {
		Emp emp = (Emp) model.asMap().get("emp");
        Integer empid = emp.getEmpId();
        List<MakedEmailVo> savedEmailVos=emailService.querysavedemail(empid);
		Map<String, Object> map = new HashMap<>();
		map.put("total", savedEmailVos.size());
        map.put("rows", savedEmailVos);
		return map;
	}
	
	/**
	 * 已发送存入垃圾箱
	 * @param emailid
	 */
	@RequestMapping("/maked_updatestate")
	@ResponseBody
	public boolean makedupdatestate(Integer data) {
		emailService.updateemailstatebyemailid(data);
		return true;
	}
	
	/**
	 * 我的收件箱删除 普通邮件
	 * @param emailid
	 * @param empid
	 * @param type
	 */
	@RequestMapping("/myreservedeletege")
	@ResponseBody
	public boolean myreservedeletege(Model model,Integer data) {
		Emp emp = (Emp) model.asMap().get("emp");
        Integer empid = emp.getEmpId();
		emailService.myreservedeletegen(data, empid);
		return true;
	}
	
	/**
	 * 我的抄送邮件删除
	 * @param model
	 * @param emailid
	 */
	@RequestMapping("/myreservedeleteco")
	@ResponseBody
	public boolean myreservedeleteco(Model model,Integer data) {
		Emp emp = (Emp) model.asMap().get("emp");
        Integer empid = emp.getEmpId();
		emailService.myreservedeletecopy(data, empid);
		return true;
	}
	
	
	/**
	 * 普通收件箱的邮件详情
	 * @param model
	 * @param emailid
	 * @param type
	 * @return
	 */
	@RequestMapping("/myreservedetailgen")
	@ResponseBody
	 public Map myreservedetailgen(Model model,Integer emailid){
		Emp emp = (Emp) model.asMap().get("emp");
        Integer empid = emp.getEmpId();
        Map map = new HashMap();       
        ReserveVo reserveVo =new ReserveVo();
        reserveVo=emailService.findreservebyempidandtypeandemailid(emailid, empid);
        model.addAttribute("myreservedetailif", reserveVo);
        List<PropertyVo> list = new ArrayList<PropertyVo>();
        list.add(new PropertyVo("发件人",reserveVo.getMakedname()));
        list.add(new PropertyVo("收件人",reserveVo.getName()));
        list.add(new PropertyVo("发件时间",reserveVo.getStartdate()));
        list.add(new PropertyVo("收件状态",reserveVo.getEnddate()));
        list.add(new PropertyVo("主题",reserveVo.getEmailtheme()));
        list.add(new PropertyVo("邮件内容",reserveVo.getEmailtext()));
        list.add(new PropertyVo("附件",reserveVo.getEmailannx()));
        list.add(new PropertyVo("邮件类型",reserveVo.getType()));
        map.put("total", list.size());
        map.put("rows", list);
        return map;
    }

	/**
	 * 抄送收件箱的邮件详情
	 * @param model
	 * @param emailid
	 * @param type
	 * @return
	 */
	@RequestMapping("/myreservedetailcopy")
	@ResponseBody
	 public Map myreservedetailcopy(Model model,Integer emailid){
		Emp emp = (Emp) model.asMap().get("emp");
        Integer empid = emp.getEmpId();
        Map map = new HashMap();       
        ReserveVo reserveVo =new ReserveVo();
        reserveVo=emailService.findreservebyempidandemailidcope(emailid, empid);
        List<PropertyVo> list = new ArrayList<PropertyVo>();
        list.add(new PropertyVo("发件人",reserveVo.getMakedname()));
        list.add(new PropertyVo("收件人",reserveVo.getName()));
        list.add(new PropertyVo("发件时间",reserveVo.getStartdate()));
        list.add(new PropertyVo("收件状态",reserveVo.getEnddate()));
        list.add(new PropertyVo("主题",reserveVo.getEmailtheme()));
        list.add(new PropertyVo("邮件内容",reserveVo.getEmailtext()));
        list.add(new PropertyVo("附件",reserveVo.getEmailannx()));
        list.add(new PropertyVo("邮件类型",reserveVo.getType()));
        map.put("total", list.size());
        map.put("rows", list);
        return map;
    }

	
	/**
	 * 收件箱页面
	 * @return
	 */
	@RequestMapping("/to_reservejsp")
	public String to_reservejsp() {
		return "to_reservejsp";
	}
	/**
	 * 收件箱查询
	 * @param model
	 * @return
	 */
	@RequestMapping("/to_selectmyreserve")
	@ResponseBody
	public Map<String, Object> to_selectmyreserve(Model model) {
		Emp emp = (Emp) model.asMap().get("emp");
        Integer empid = emp.getEmpId();
        List<ReserveVo> reserveVos=emailService.queryreservebyempid(empid);
		Map<String, Object> map = new HashMap<>();
		map.put("total", reserveVos.size());
        map.put("rows", reserveVos);
		return map;
	}
	
	/**
	 * 通讯录页面
	 * @param model
	 * @return
	 */
	@RequestMapping("/totelcard")
	public String totelcard(Model model) {
		return "to_addbook";
	}
	
	/**
	 * 写信页面
	 * @param model
	 * @return
	 * @throws ParseException 
	 */
	@RequestMapping("/to_writeemail")
	public String to_writeemail(Model model) throws ParseException {
		EmpUseByemailVo empUseByemailVo=new EmpUseByemailVo();
		Emp emp = (Emp) model.asMap().get("emp");
	    Integer empid =emp.getEmpId();
		empUseByemailVo=emailService.selectempinfoEmail(empid);
		model.addAttribute("empUseByemailVo", empUseByemailVo);
		return "to_writeemail";
	}
	
	/**
	 * 我的已发送详情
	 * @param model
	 * @param emailid
	 * @return
	 */
	@RequestMapping("/makeddetailif")
	@ResponseBody
	 public Map makeddetailif(Integer emailid){
        Map map = new HashMap();       
        MakedEmailVo makedEmailVo=emailService.mymakedemaildetail(emailid);
        List<PropertyVo> list = new ArrayList<PropertyVo>();
        list.add(new PropertyVo("发件人:",makedEmailVo.getName()));
        list.add(new PropertyVo("收件人:",makedEmailVo.getReservename()));
        list.add(new PropertyVo("发件时间",makedEmailVo.getStartdate()));
        list.add(new PropertyVo("收件状态",makedEmailVo.getReservestate()));
        list.add(new PropertyVo("抄送",makedEmailVo.getCopylist()));
        list.add(new PropertyVo("主题",makedEmailVo.getEmailTheme()));
        list.add(new PropertyVo("邮件内容",makedEmailVo.getEmailText()));
        list.add(new PropertyVo("附件",makedEmailVo.getEmailAnnex()));
        map.put("total", list.size());
        map.put("rows", list);
        return map;
    }
	
	/**
	 * 暂存箱详情
	 * @param emailid
	 * @return
	 */
	@RequestMapping("/savedetailif")
	@ResponseBody
	 public Map savedetailif(Integer emailid){
        Map map = new HashMap();       
        MakedEmailVo makedEmailVo=emailService.mysavedemaildetail(emailid);
        List<PropertyVo> list = new ArrayList<PropertyVo>();
        list.add(new PropertyVo("发件人",makedEmailVo.getName()));
        list.add(new PropertyVo("收件人",makedEmailVo.getReservename()));
        list.add(new PropertyVo("最后编辑时间",makedEmailVo.getStartdate()));
        list.add(new PropertyVo("收件状态",makedEmailVo.getReservestate()));
        list.add(new PropertyVo("抄送",makedEmailVo.getCopylist()));
        list.add(new PropertyVo("主题",makedEmailVo.getEmailTheme()));
        list.add(new PropertyVo("邮件内容",makedEmailVo.getEmailText()));
        list.add(new PropertyVo("附件",makedEmailVo.getEmailAnnex()));
        map.put("total", list.size());
        map.put("rows", list);
        return map;
    }
	
	/**
	 * 我的暂存箱删除
	 * @param data
	 */
	@RequestMapping("/savedelete")
	@ResponseBody
	public void savedelete(Integer data) {
		emailService.savedeletedocopy(data);
		emailService.savedeletedoout(data);
		emailService.savedeletedoemial(data);
	}
	
	/**
	 * 垃圾箱恢复
	 * @param data
	 */
	@RequestMapping("/deleteback")
	@ResponseBody
	public boolean deleteback(Integer data) {
		emailService.deleteback(data);
		return true;
	}
	
	/**
	 * 垃圾箱永久删除
	 * @param data
	 */
	@RequestMapping("/deletedelete")
	@ResponseBody
	public boolean deletedelete(Integer data) {
		emailService.deletedelete(data);
		return true;
	}
	
	/**
	 * 新建邮件时获取收件人
	 * @param model
	 * @return
	 */
	@RequestMapping("/newemailemps")
	@ResponseBody
	public Map<String, Object> newemailemps(Model model) {
		Emp emp = (Emp) model.asMap().get("emp");
        Integer empid = emp.getEmpId();
        List<NewemailVo> newemailVos=emailService.newemailVolist(empid);
		Map<String, Object> map = new HashMap<>();
		map.put("total", newemailVos.size());
        map.put("rows", newemailVos);
		return map;
	}
	
	/**
	 * 新建邮件时获取抄送人
	 * @param model
	 * @return
	 */
	@RequestMapping("/newemailempscopy")
	@ResponseBody
	public Map<String, Object> newemailempscopy(Model model) {
		Emp emp = (Emp) model.asMap().get("emp");
        Integer empid = emp.getEmpId();
        List<NewcopyemailVo> newcopyemailVos=emailService.newemailVocopylist(empid);
		Map<String, Object> map = new HashMap<>();
		map.put("total", newcopyemailVos.size());
        map.put("rows", newcopyemailVos);
		return map;
	}
	/**
	 * 树
	 */
	@RequestMapping("/get_tree")
	@ResponseBody
	public List<NewcopyemailVo> tree(Model model) {
		Emp emp = (Emp) model.asMap().get("emp");
        Integer empid = emp.getEmpId();
        List<NewcopyemailVo> newcopyemailVos=emailService.newemailVocopylist(empid);
		return newcopyemailVos;
	}
	
	/**
	 * 今天生日的员工
	 * @param model
	 * @return
	 * @throws ParseException 
	 */
	@RequestMapping("/todaybirth")
	@ResponseBody
	public Map<String, Object> todaybirth(Model model) throws ParseException {	
		Emp emp = (Emp) model.asMap().get("emp");
        Integer empid = emp.getEmpId();emailService.todaysbirthemp(empid);
        List<BirthempVo> birthempVos=emailService.todaysbirthemp(empid);
		Map<String, Object> map = new HashMap<>();
		map.put("total", birthempVos.size());
        map.put("rows", birthempVos);
		return map;
	}
	
	/**
	 * 明天生日的员工
	 * @param model
	 * @return
	 * @throws ParseException
	 */
	@RequestMapping("/tomorrowbirth")
	@ResponseBody
	public Map<String, Object> tomorrowbirth(Model model) throws ParseException {	
		Emp emp = (Emp) model.asMap().get("emp");
        Integer empid = emp.getEmpId();
        List<BirthempVo> birthempVos=emailService.tomorrowsbirthemp(empid);
		Map<String, Object> map = new HashMap<>();
		map.put("total", birthempVos.size());
        map.put("rows", birthempVos);
		return map;
	}
	
	/**
	 * 后天生日的员工
	 * @param model
	 * @return
	 * @throws ParseException
	 */
	@RequestMapping("/thethirdbirth")
	@ResponseBody
	public Map<String, Object> thethirdbirth(Model model) throws ParseException {	
		Emp emp = (Emp) model.asMap().get("emp");
        Integer empid = emp.getEmpId();
        List<BirthempVo> birthempVos=emailService.thethirdbirthemp(empid);
		Map<String, Object> map = new HashMap<>();
		map.put("total", birthempVos.size());
        map.put("rows", birthempVos);
		return map;
	}
	
	/**
	 * 生日朋友详情
	 * @param empid
	 * @return
	 */
	@RequestMapping("/birthdetail")
	@ResponseBody
	 public Map birthdetail(Integer empid){
        Map map = new HashMap();       
        BirthdetailVo birthdetailVo=emailService.getbirthdetail(empid);
        List<PropertyVo> list = new ArrayList<PropertyVo>();
        list.add(new PropertyVo("姓名",birthdetailVo.getEmpname()));
        list.add(new PropertyVo("部门",birthdetailVo.getDept()));
        list.add(new PropertyVo("职位",birthdetailVo.getPosition()));
        list.add(new PropertyVo("性别",birthdetailVo.getSex()));
        list.add(new PropertyVo("电话",birthdetailVo.getTel()));
        list.add(new PropertyVo("QQ",birthdetailVo.getQq()));
        list.add(new PropertyVo("生日",birthdetailVo.getBirth()));
        list.add(new PropertyVo("邮箱",birthdetailVo.getEmail()));
        map.put("total", list.size());
        map.put("rows", list);
        return map;
    }
	
	/**
	 * 写信
	 * @param model
	 * @param name
	 * @param copy
	 * @param theme
	 * @param text
	 */
	@RequestMapping("/postemail")
	@ResponseBody
	public boolean postemail(Model model,String name,String copy,String theme,String text) {
		Emp emp = (Emp) model.asMap().get("emp");
		Integer empid = emp.getEmpId();
		emailService.postemailproccess(empid, name, copy, theme, text);	
		return true;
	}
	
	/**
	 * 发送没有抄送人的邮件
	 * @param model
	 * @param name
	 * @param copy
	 * @param theme
	 * @param text
	 * @return
	 */
	@RequestMapping("/postemailnocopy")
	@ResponseBody
	public boolean postemailnocopy(Model model,String name,String copy,String theme,String text) {
		Emp emp = (Emp) model.asMap().get("emp");
		Integer empid = emp.getEmpId();
		emailService.postemailwithoutcopy(empid, name, theme, text);
		return true;
	}
	
	/**
	 * 根据id获取员工姓名
	 * @param empid
	 * @return
	 */
	@RequestMapping("/getnamebyempid")
	@ResponseBody
	public String getempnamebyid(Integer empid) {
		return emailService.getempnamebyempid(empid);
	}
	
	/**
	 * 发送生日邮件给朋友
	 * @param model
	 * @param reserveemp
	 * @param theme
	 * @param text
	 * @return
	 */
	@RequestMapping("/postemailforbir")
	@ResponseBody
	public boolean postemailforbir(Model model,String reserveemp,String theme,String text) {
		Emp emp = (Emp) model.asMap().get("emp");
		Integer empid = emp.getEmpId();
		emailService.postemailwithoutcopy(empid, reserveemp, theme, text);
		return true;
	}
	
	/**
	 * 暂存邮件
	 * @param model
	 * @param name
	 * @param copy
	 * @param theme
	 * @param text
	 * @return
	 */
	@RequestMapping("/savepostemail")
	@ResponseBody
	public boolean savepostemail(Model model,String name,String copy,String theme,String text) {
		Emp emp = (Emp) model.asMap().get("emp");
		Integer empid = emp.getEmpId();
		emailService.savepostemailproccess(empid, name, copy, theme, text);	
		return true;
	}
	
	/**
	 * 暂存没有抄送人的邮件
	 * @param model
	 * @param name
	 * @param copy
	 * @param theme
	 * @param text
	 * @return
	 */
	@RequestMapping("/savepostemailnocopy")
	@ResponseBody
	public boolean savepostemailnocopy(Model model,String name,String copy,String theme,String text) {
		Emp emp = (Emp) model.asMap().get("emp");
		Integer empid = emp.getEmpId();
		emailService.savepostemailwithoutcopy(empid, name, theme, text);
		return true;
	}
	
	/**
	 * 发送暂存的邮件
	 * @param model
	 * @param name
	 * @param copy
	 * @param theme
	 * @param text
	 * @return
	 */
	@RequestMapping("/saveforemail")
	@ResponseBody
	public boolean saveforemail(Model model,Integer emailid,String emailtheme,String emailtext) {
		emailService.saveforemail(emailid, emailtheme, emailtext);
		return true;
	}
	
	/**
	 * 暂存的邮件保存编辑
	 * @param model
	 * @param name
	 * @param copy
	 * @param theme
	 * @param text
	 * @return
	 */
	@RequestMapping("/saveforsavee")
	@ResponseBody
	public boolean saveforsavee(Model model,Integer emailid,String emailtheme,String emailtext) {
		emailService.saveforsave(emailid, emailtheme, emailtext);
		return true;
	}
	
	/**
	 * 回复已收件
	 * @param model
	 * @param emailid
	 * @param emailtheme
	 * @param emailtext
	 * @return
	 */
	@RequestMapping("/forbackemail")
	@ResponseBody
	public boolean forbackemail(Model model,String name,String emailtheme,String emailtext) {
		Emp emp = (Emp) model.asMap().get("emp");
		Integer empid = emp.getEmpId();
		
		return true;
	}
}
